package cn.marinda.resturants.controller;

import cn.marinda.resturants.entity.Permission;
import cn.marinda.resturants.global.Pages;
import cn.marinda.resturants.service.PermissionsService;
import cn.marinda.resturants.utils.ApiResult;
import cn.marinda.resturants.utils.PagesResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Resource
    private PermissionsService permissionsService;


    /** 列表分页
     *
     * @date 2021/12/1
     * @return 结果
     */
    @RequestMapping("/pages")
    private PagesResult selectPages(@RequestParam("page") int page, @RequestParam("limit") int limit)
    {
        IPage<Permission> permissionIPage = permissionsService.selectPages(new Pages<Permission>(page,limit));
        return new PagesResult(0,permissionIPage.getRecords(),(int)permissionIPage.getTotal(),permissionIPage.getSize());
    }
    /**
     * 查询所有权限
     *
     * @return 权限列表
     */
    @RequestMapping("/queryList")
    public ApiResult queryList() {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,permissionsService.queryList());
    }

    /**
     * 选择权限
     *
     * @param uid uid
     * @return {@link ApiResult}
     */
    @RequestMapping("/index")
    public ApiResult selectPermission(String uid) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE, Arrays.asList(permissionsService.selectPermissionUid(uid)));
    }

    /**
     * 添加一个权限
     *
     * @param permission 权限
     * @return 结果
     */
    @RequestMapping("/add")
    public ApiResult addPermission(Permission permission) {
        return permissionsService.addPermission(permission) ? new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) :  new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);
    }

    /**
     * 删除一个权限
     *
     * @param uid uid
     * @return 结果
     */
    @RequestMapping("/delete")
    public ApiResult deletePermission(long uid) {
        return permissionsService.deletePermission(uid) ? new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) :  new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);

    }

    /**
     * 子权限
     *
     * @param uid uid
     * @return 子权限
     */
    @RequestMapping("/childUid")
    public ApiResult selectChildUid(long uid) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,permissionsService.selectChildUid(uid));

    }

    /**
     * 查询名字
     *
     * @param uid uid
     * @return 名字
     */
    @RequestMapping("/name")
    public ApiResult selectName(long uid) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,permissionsService.selectName(uid));

    }

    /**
     * 查询父权限
     *
     * @param uid uid
     * @return 父级权限
     */
    @RequestMapping("/parentUid")
    public ApiResult selectParentUid(long uid) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,permissionsService.selectParentUid(uid));

    }

    /**
     * 乐观锁修改权限
     *
     * @param permission 权限
     * @return 结果
     */
    @RequestMapping("/update")
    public ApiResult updatePermission(Permission permission) {
        return permissionsService.updatePermission(permission) ? new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) :  new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);

    }
}
